home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / MacMarlais 0.5.9d46 / Examples / dylan-manual-examples.txt < prev    next >
Encoding:
Text File  |  1995-03-13  |  27.7 KB  |  1,334 lines  |  [TEXT/Mrls]

  1. This document contains the examples from "Dylan: an object-oriented dynamic
  2. language".
  3.  
  4. copyright 1992, Apple Computer
  5.  
  6.  
  7.  
  8. Page 27
  9.  
  10.    ? "abc"
  11.    "abc"
  12.    ? 123
  13.    123
  14.    ? foo:
  15.    foo:
  16.    ? #\a
  17.    #\a
  18.    ? #t
  19.    #t
  20.    ? #f
  21.    #f
  22.    ? (quote foo)
  23.    foo
  24.    ? 'foo
  25.    foo
  26.    ? '(1 2 3)
  27.    (1 2 3)
  28.  
  29.  
  30. Page 28-29
  31.  
  32.    ? <window>
  33.    {the class <window>}
  34.    ? concatenate
  35.    {the generic function concatenate}
  36.    ? (define my-variable 25)
  37.    my-variable
  38.    ? my-variable
  39.    25
  40.    ? (bind ((x 50))
  41.        (+ x x))
  42.    100
  43.    ? (setter element)
  44.    {the generic function (setter element)}
  45.    ? (define (setter my-variable) 20) 
  46.    (setter my-variable)
  47.    ? (setter my-variable)
  48.    20
  49.  
  50. Page 29
  51.  
  52.    ? (+ 3 4)
  53.    7
  54.    ? (* my-variable 3)
  55.    75
  56.    ? (* (+ 3 4) 5)
  57.    35
  58.    ? ((if #t + *) 4 5)
  59.    9
  60.  
  61. Page 30
  62.  
  63.    ; Creates and initializes a module variable
  64.    (define my-variable 25)
  65.    ; Sets the value to 12
  66.    (set! my-variable 12)
  67.    ; Returns 30. Uses lexical variables x and y.
  68.    (bind ((x 10) (y 20))
  69.       (+ x y))
  70.    ; Creates an anonymous method, which expects 2 
  71.    ; numeric arguments.
  72.    (method ((a <number>) (b <number>))
  73.       (list (- a b) (+ a b)))
  74.  
  75. Page 30
  76.  
  77.    ? (values 1 2 3)
  78.    1
  79.    2
  80.    3
  81.    ? (define-method edges ((center <number>) (radius <number>))
  82.        (values (- center radius) (+ center radius)))
  83.    edges
  84.    ? (edges 100 2)
  85.    98
  86.    102
  87.  
  88. Page 32
  89.  
  90.    ? foo
  91.    error: unbound variable foo
  92.    ? (define foo 10)
  93.    foo
  94.    ? foo
  95.    10
  96.    ? (+ foo 100)
  97.    110
  98.    ? bar
  99.    error: unbound variable bar
  100.    ? (define bar foo)
  101.    bar
  102.    ? bar
  103.    10
  104.    ? (define foo 20)
  105.    warning: redefining variable foo
  106.    ? foo
  107.    20
  108.    ? bar
  109.    10
  110.    ? (+ foo bar)
  111.    30
  112.  
  113. Page 33
  114.  
  115.    ? (bind ((number1 20)
  116.             (number2 30))
  117.        (+ number1 number2))
  118.    50
  119.  
  120. Page 33
  121.  
  122.    ? (bind ((x 20)
  123.             (y (+ x x)))
  124.        (+ y y))
  125.    80
  126.  
  127. Page 33
  128.  
  129.    ? (define foo 10)
  130.    foo
  131.    ? (+ foo foo)
  132.    20
  133.    ? (bind ((foo 35))
  134.        (+ foo foo))
  135.    70
  136.    ? (bind ((foo 20))
  137.        (bind ((foo 50))
  138.          (+ foo foo)))
  139.    100
  140.  
  141. Page 34
  142.  
  143.    ? (bind (((x <integer>) (sqrt 2)))
  144.            x)
  145.    error: 1.4142135623730951 is not an instance of <integer>
  146.  
  147.  
  148. Page 34
  149.  
  150.    ? (bind ((foo bar baz (values 1 2 3)))
  151.        (list foo bar baz))
  152.    (1 2 3)
  153.    ? (define-method opposite-edges ((center <number>)
  154.                                     (radius <number>))
  155.        (bind ((min max (edges center radius)))
  156.          (values max min)))
  157.    opposite-edges
  158.    ? (opposite-edges 100 2)
  159.    102
  160.    98
  161.  
  162. Page 34
  163.  
  164.    ? (bind ((x 10)
  165.             (y 20))
  166.        (bind ((x y (values y x)))
  167.          (list x y)))
  168.    (20 10)
  169.  
  170. Page 34
  171.  
  172.    ? (bind ((#rest nums (edges 100 2)))
  173.        nums)
  174.    (98 102)
  175.  
  176. Page 41
  177.  
  178.    ? (double 10)
  179.    error: unbound variable double.
  180.  
  181. Page 41
  182.  
  183.    ? (define-method double ((thing <number>))
  184.        (+ thing thing))
  185.    double
  186.    ? double
  187.    {the generic function double}
  188.    ? (double 10)
  189.    20
  190.  
  191. Page 41
  192.  
  193.    ? (double "the rain in Spain.")
  194.    error: no method for {the generic function double} was found
  195.           for the arguments ("the rain in Spain.")
  196.  
  197. Page 41
  198.  
  199.    ? (define-method double ((thing <sequence>))
  200.        (concatenate thing thing))
  201.    double
  202.    ? (double "the rain in Spain.")
  203.    "the rain in Spain.the rain in Spain."
  204.    ? (double '(a b c))
  205.    (a b c a b c)
  206.  
  207. Page 43
  208.  
  209.    ? (define-method show-rest (a #rest b)
  210.        (print a)
  211.        (print b)
  212.        #t)
  213.    show-rest
  214.    ? (show-rest 10 20 30 40)
  215.    10
  216.    (20 30 40)
  217.    #t
  218.    ? (show-rest 10)
  219.    10
  220.    ()
  221.    #t
  222.  
  223. Page 44
  224.  
  225.    (define-method percolate (#key (brand 'maxwell-house)
  226.                                   (cups 4)
  227.                                   (strength 'strong))
  228.      (make-coffee brand cups strength))
  229.    (define-method layout (widget #key (position: the-pos)
  230.                                       (size: the-size))
  231.      (bind ((the-sibling (sibling widget)))
  232.       (unless (= the-pos (position the-sibling))
  233.         (align-objects widget the-sibling the-pos the-size))
  234.  
  235. Page 44
  236.  
  237.    (percolate brand: 'folgers cups: 10)
  238.    (percolate strength: 'weak
  239.               brand: 'tasters-choice
  240.               cups: 1)
  241.    (layout my-widget position: (point 10 10)
  242.                      size: (point 30 50))
  243.    (layout my-widget size: (query-user-for-size))
  244.  
  245. Page 45
  246.  
  247.    ? (define-method show-keys (req1 req2 #key foo)
  248.        (format #t "requireds: ~a ~a~%" req1 req2)
  249.        (format #t "key: ~a" foo)
  250.        #t)
  251.    show-keys
  252.    ? (show-keys 'one 'two foo: 'three)
  253.    requireds: one two
  254.    key: three
  255.    #t
  256.    ? (show-keys foo: 'three)
  257.    requireds: foo: three
  258.    key: #f
  259.    #t
  260.  
  261. Page 46
  262.  
  263.    ? (define-method label ((x <object>) #key price)
  264.       (list price x))
  265.    label
  266.    ? (define-method label ((x <sequence>) #key unit-price)
  267.       (add x (* unit-price (length x))))
  268.    label
  269.    ? (define-method label ((x <list>) #rest info #key calories)
  270.       (add x calories))
  271.    label
  272.    ? (label 'grape price: 189 unit-price: 2)
  273.    error:  illegal keyword argument unit-price:.  Accepted keyword arguments are (price:).
  274.    ? (label 'grape price: 189)
  275.    (189 grape)
  276.    ? (label (vector 3 4 5) price: 189 unit-price: 2)
  277.    #(6 3 4 5)
  278.    ? (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
  279.    error:  illegal keyword argument protein:.  Accepted keyword arguments are (price: unit-price:).
  280.    ? (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
  281.    (9 3 4 5)
  282.  
  283. Page 46
  284.  
  285.    ? (define-method test (the-req #rest the-rest
  286.                                   #key a b)
  287.        (print the-req)
  288.        (print the-rest)
  289.        (print a)
  290.        (print b))
  291.    test
  292.    ? (test 1 a: 2 b: 3 c: 4)
  293.    1
  294.    (a: 2 b: 3 c: 4)
  295.    2
  296.    3
  297.  
  298. Page 49
  299.  
  300.    (define-class <point> (<object>)
  301.      horizontal
  302.      vertical)
  303.  
  304. Page 49
  305.  
  306.    (horizontal my-point)
  307.  
  308. Page 49
  309.  
  310.    ((setter horizontal) my-point 10)
  311.  
  312. Page 50
  313.  
  314.    (set! (horizontal my-point) 10)
  315.  
  316. Page 51   
  317.    
  318.    ? (define-class <menu> (<object>)
  319.        title
  320.        action)
  321.  
  322. Page 55
  323.  
  324.    ? (define-class <rectangle> (<object>)
  325.         (top type: <integer>
  326.              init-value: 0
  327.              init-keyword: top:)
  328.         (left type: <integer>
  329.               init-value: 0
  330.               init-keyword: left:)
  331.         (bottom type: <integer>
  332.                 init-value: 100
  333.                 init-keyword: bottom:)
  334.         (right type: <integer>
  335.                init-value: 100
  336.                init-keyword: right:))
  337.    <rectangle>
  338.    ? <rectangle>
  339.    {the class <rectangle>}
  340.    ? (define my-rectangle (make <rectangle> top: 50 left: 50))
  341.    my-rectangle
  342.    ? (top my-rectangle)
  343.    50
  344.    ? (bottom my-rectangle)
  345.    100
  346.    ? (set! (bottom my-rectangle) 55)
  347.    55
  348.    ? (bottom my-rectangle)
  349.    55
  350.    ? (set! (bottom my-rectangle) 'foo)
  351.    error: foo is not an instance of <integer> while executing (setter bottom).
  352.  
  353.  
  354. Page 58
  355.    
  356.    (define-class <view> (<object>)
  357.      (position allocation: instance)
  358.      ...)
  359.    
  360.    (define-class <displaced-view> (<view>)
  361.      (position allocation: virtual)
  362.      ...)
  363.    
  364.    (define-method position ((v <displaced-view>))
  365.      (displace-transform (next-method v)))
  366.    
  367.    (define-method (setter position) ((v <displaced-view>)
  368.                                      new-position)
  369.      (next-method v (undisplace-transform new-position)))
  370.  
  371. Page 59
  372.  
  373.    (define-class <shape> (<view>)
  374.      (image allocation: virtual)
  375.      (cached-image allocation: instance init-value: #f)
  376.      ...)
  377.    
  378.    (define-method image ((shape <shape>))
  379.      (or (cached-image shape)
  380.          (set! (cached-image shape) (compute-image shape))))
  381.    
  382.    (define-method (setter image) ((shape <shape>) new-image)
  383.      (set! (cached-image shape) new-image))
  384.  
  385. Page 61
  386.  
  387.    ? (define foo 10)
  388.    10
  389.    ? foo             ;this is a variable
  390.    10                ;this is the variableÕs contents
  391.    ? (set! foo (+ 10 10))
  392.    20
  393.    ? foo
  394.    20
  395.    ? (setter element)                   ;this is a variable
  396.    {generic function (setter element)}  ;the variableÕs contents
  397.    ? (set! (setter element) %set-element)
  398.    {primitive function %set-element}
  399.    ? (id? (setter element) %set-element)
  400.    #t
  401.  
  402. Page 62
  403.  
  404.    ? (define foo (vector 'a 'b 'c 'd))
  405.    foo
  406.    ? foo
  407.    #(a b c d)
  408.    ? (element foo 2)
  409.    c
  410.    ? (set! (element foo 2) 'sea)
  411.    sea
  412.    ? (element foo 2)
  413.    sea
  414.    ? foo
  415.    #(a b sea d)
  416.  
  417. Page 64
  418.  
  419.    ? (define-method test ((thing <object>))
  420.        (if thing
  421.            #t
  422.            #f))
  423.    test
  424.    ? (test 'hello)
  425.    #t
  426.    ? (test #t)
  427.    #t
  428.    ? (test #f)
  429.    #f
  430.    
  431.    ? (define-method double-negative ((num <number>))
  432.         (if (< num 0)
  433.             (+ num num)
  434.             num))
  435.    double-negative
  436.    ? (double-negative 11)
  437.    11
  438.    ? (double-negative -11)
  439.    -22
  440.  
  441. Page 65
  442.  
  443.    ? (define-method show-and-tell ((thing <object>))
  444.         (if thing
  445.             (begin
  446.                (print thing)
  447.                #t)
  448.             #f))
  449.    show-and-tell
  450.    ? (show-and-tell "hello")
  451.    hello
  452.    #t
  453.  
  454. Page 65
  455.  
  456.    (when (bonus-illuminated? pinball post)
  457.        (add-bonus-score current-player 100000))
  458.  
  459. Page 65
  460.  
  461.    (unless (detect-gas? nose)
  462.        (light match))
  463.  
  464. Page 66
  465.    
  466.    (cond ((< new-position old-position)
  467.             "the new position is less")
  468.           ((= new-position old-position)
  469.            "the positions are equal")
  470.           (else: "the new position is greater"))
  471.  
  472. Page 67
  473.  
  474.    (case (career-choice student)
  475.       ((art music drama)
  476.        (print "Don’t quit your day job."))
  477.       ((literature history linguistics)
  478.        (print "That really is fascinating."))
  479.       ((science math engineering)
  480.        (print "Say, can you fix my VCR?"))
  481.       (else: "I wish you luck."))
  482.  
  483. Page 67
  484.  
  485.    (select my-object instance?
  486.      ((<window> <view> <rectangle>) "itÕs a graphic object")
  487.      ((<number> <list> <sequence>) "itÕs something computational")
  488.      (else: "DonÕt know what it is"))
  489.  
  490. Page 68
  491.  
  492.    ? (if #t
  493.          (print "it was true")
  494.          #t
  495.          #f)
  496.    error:  too many arguments to if.
  497.    ? (if #t
  498.          (begin (print "it was true")
  499.                 #t)
  500.          #f)
  501.    "it was true"
  502.    #t
  503.  
  504. Page 69
  505.  
  506.    (define-method factorial ((n <integer>))
  507.       (for ((i n (- i 1))   ;variable clause 1
  508.             (v 1 (* v i)))  ;variable clause 2
  509.            ((<= i 0) v)))   ;end test and result
  510.  
  511. Page 69
  512.  
  513.    (define-method first-even ((s <sequence>))
  514.      (for-each ((number s))
  515.                ((even? number) number)
  516.                                 ; No body forms needed
  517.         ))
  518.  
  519. Page 70
  520.  
  521.    (define-method schedule-olympic-games ((cities <sequence>)
  522.                                           (start-year <number>))
  523.       (for-each ((year (range from: start-year by: 4))
  524.                  (city cities))
  525.                 ()              ; No end test needed.
  526.          (schedule-game city year)))
  527.  
  528. Page 70
  529.  
  530.    ? (begin
  531.        (dotimes (i 6) (print "bang!"))
  532.        (print "click!"))
  533.    bang!
  534.    bang!
  535.    bang!
  536.    bang!
  537.    bang!
  538.    bang!
  539.    click!
  540.  
  541. Page 71
  542.  
  543.    ? (define-method first-even ((seq <sequence>))
  544.        (bind-exit (exit)
  545.          (do (method (item)
  546.                 (when (even? item)
  547.                   (exit item)))
  548.               seq)))
  549.    first-even
  550.    ? (first-even '(1 3 5 4 7 9 10))
  551.    4
  552.  
  553. Page 72
  554.  
  555.    ? +
  556.    {the generic function +}
  557.    ? '+
  558.    +
  559.    ? (quote +)
  560.    +
  561.    ? ''+
  562.    (quote +)
  563.    ? (+ 10 10)
  564.    20
  565.    ? '(+ 10 10)
  566.    (+ 10 10)
  567.    ? (quote (+ 10 10))
  568.    (+ 10 10)
  569.  
  570. Page 73
  571.  
  572.    ? (apply + 1 '(2 3))
  573.    6
  574.    ? (+ 1 2 3)
  575.    6
  576.    ? (define math-functions (list + * / -))
  577.    math-functions
  578.    ? math-functions
  579.    ({method +} {method *} {method /} {method -})
  580.    ? (first math-functions)
  581.    {method +}
  582.    ? (apply (first math-functions) 1 2 '(3 4))
  583.    10
  584.  
  585. Page 79
  586.  
  587.    ? (method (num1 num2)
  588.        (+ num1 num2))
  589.    {an anonymous method}
  590.  
  591. Page 80
  592.  
  593.    ;the second argument to SORT is the test function
  594.    ? (sort person-list
  595.            (method (person1 person2)
  596.              (< (age person1)
  597.                 (age person2))))
  598.    ? (bind ((double (method (number)
  599.                       (+ number number))))
  600.        (double (double 10)))
  601.    40
  602.  
  603. Page 80
  604.  
  605.    ? (define-method double ((my-method <function>))
  606.        (method (#rest args)
  607.          (apply my-method args)
  608.          (apply my-method args)
  609.          #f))
  610.    double
  611.    ? (define print-twice (double print))
  612.    print-twice
  613.    ? print-twice
  614.    {an anonymous method}
  615.    ? (print-twice "The rain in Spain. . .")
  616.    The rain in Spain. . .The rain in Spain. . .
  617.    #f
  618.    ? (print-twice 55)
  619.    5555
  620.    #f
  621.  
  622. Page 81
  623.  
  624.    ? (define-method root-mean-square ((s <sequence>))
  625.         (bind-methods ((average (numbers)
  626.                          (/ (reduce1 + numbers)
  627.                             (length numbers)))
  628.                        (square (n) (* n n)))
  629.            (sqrt (average (map square s)))))
  630.    root-mean-square
  631.    ? (root-mean-square '(5 6 6 7 4))
  632.    5.692099788303083
  633.  
  634. Page 81
  635.  
  636.    ? (define-method newtons-sqrt (x)
  637.         (bind-methods ((sqrt1 (guess)
  638.                           (if (close? guess) 
  639.                               guess 
  640.                               (sqrt1 (improve guess))))
  641.                        (close? (guess)
  642.                           (< (abs (- (* guess guess) x)) .0001))
  643.                        (improve (guess)
  644.                           (/ (+ guess (/ x guess)) 2)))
  645.                (sqrt1 1)))
  646.    newtons-sqrt
  647.    ? (newtons-sqrt 25)
  648.    5.000000000053723
  649.  
  650. Page 82
  651.  
  652.    ? (define-method double ((thing <number>))
  653.        (+ thing thing))
  654.    double
  655.  
  656. Page 82
  657.  
  658.    ? (double 10)
  659.    20
  660.    ? (double 4.5)
  661.    9.0
  662.  
  663. Page 82
  664.  
  665.    ? (define-method double ((thing <integer>))
  666.        (* thing 2))
  667.    double
  668.  
  669. Page 82
  670.  
  671.    ? (define-method double ((thing (singleton 'cup)))
  672.        'pint)
  673.    double
  674.    ? (double 'cup)
  675.    pint
  676.  
  677. Page 83
  678.  
  679.    ? (define-method double ((num <float>))
  680.        (print "doubling a floating-point number")
  681.        (next-method))
  682.    double
  683.    ? (double 10.5)
  684.    doubling a floating-point number
  685.    21.0
  686.  
  687. Page 85
  688.  
  689.    (define-method show ((device <window>) (thing <character>))
  690.      ...)
  691.    
  692.    (define-method show ((device <window>) (thing <string>))
  693.      ...)
  694.    
  695.    (define-method show ((device <window>) (thing <rectangle>))
  696.      . . .)
  697.    
  698.    (define-method show ((device <file>) (thing <character>))
  699.      . . .)
  700.    
  701.    (define-method show ((device <file>) (thing <string>))
  702.      . . .)
  703.  
  704. Page 86
  705.  
  706.    ? (make <generic-function> required: 3)
  707.    {an anonymous generic function}
  708.    ? (make <generic-function> required: 3
  709.                               debug-name: 'foo)
  710.    {the generic function foo}
  711.    ? (define expand
  712.        (make <generic-function> required: 1 debug-name: 'expand))
  713.    {the generic function expand}
  714.    ? (expand 55)
  715.    error: no applicable method for 55 in {the generic function expand}
  716.  
  717. Page 97
  718.  
  719.    ? (define-method double ((thing (singleton 'cup)))
  720.        'pint)
  721.    double
  722.    ? (double 'cup)
  723.    pint
  724.    ? (double 10)
  725.    20
  726.  
  727. Page 98
  728.  
  729.    ? (define-method factorial ((num <integer>))
  730.        (* num (factorial (- num 1))))
  731.    factorial
  732.    ? (define-method factorial ((num (singleton 0)))
  733.         1)
  734.    factorial
  735.    ? (factorial 5)
  736.    120
  737.  
  738. Page 100
  739.  
  740.    ? (do (method (a b) (print (+ a b)))
  741.          '(100 100 200 200)
  742.          '(1 2 3 4))
  743.    101
  744.    102
  745.    203
  746.    204
  747.    #f
  748.  
  749. Page 101
  750.  
  751.    ? (map +
  752.          '(100 100 200 200)
  753.          '(1 2 3 4))
  754.    (101 102 203 204)
  755.  
  756. Page 101
  757.  
  758.    ? (map-as <vector> +
  759.          '(100 100 200 200)
  760.          '(1 2 3 4))
  761.    #(101 102 203 204)
  762.  
  763. Page 101
  764.  
  765.    ? (define x '(100 100 200 200))
  766.    x
  767.    ? (map-into x + '(1 2 3 4))
  768.    (101 102 203 204)
  769.    ? x
  770.    (101 102 203 204)
  771.  
  772. Page 102
  773.  
  774.    ? (any? > '(1 2 3 4) '(5 4 3 2))
  775.    #t
  776.    ? (any? even? '(1 3 5 7))
  777.    #f
  778.  
  779. Page 102
  780.  
  781.    ? (every? > '(1 2 3 4) '(5 4 3 2))
  782.    #f
  783.    ? (every? odd? '(1 3 5 7))
  784.    #t
  785.  
  786. Page 102
  787.  
  788.    ? (define high-score 10)
  789.    high-score
  790.    ? (reduce max high-score '(3 1 4 1 5 9))
  791.    10
  792.    ? (reduce max high-score '(3 12 9 8 8 6))
  793.    12
  794.  
  795. Page 103
  796.  
  797.    ? (reduce1 + '(1 2 3 4 5))
  798.    15
  799.  
  800. Page 103
  801.  
  802.    ? (define flavors #(chocolate pistachio pumpkin))
  803.    flavors
  804.    ? (member? 'chocolate flavors)
  805.    #t
  806.    ? (member? 'banana flavors)
  807.    #f
  808.  
  809. Page 103
  810.  
  811.    ? flavors
  812.    (chocolate pistachio pumpkin)
  813.    ? (find-key flavors has-nuts?)
  814.    1
  815.    ? (element flavors 1)
  816.    pistachio
  817.  
  818. Page 104
  819.  
  820. ? (define numbers (list 10 13 16 19))
  821. numbers
  822. ? (replace-elements! numbers odd? double)
  823. (10 26 16 38)
  824.  
  825. Page 104
  826.  
  827. ? (define x (list 'a 'b 'c 'd 'e 'f))
  828. x
  829. ? (fill! x 3 start: 2)
  830. (a b 3 3 3 3)
  831.  
  832. Page 105
  833.  
  834.    ? (define numbers '(3 4 5))
  835.    numbers
  836.    ? (add numbers 1)
  837.    (1 3 4 5)
  838.    ? numbers
  839.    (3 4 5)
  840.  
  841. Page 105
  842.  
  843.    ? (define numbers (list 3 4 5))
  844.    numbers
  845.    ? (add! numbers 1)
  846.    (1 3 4 5)
  847.  
  848. Page 105
  849.  
  850.    ? (add-new '(3 4 5) 1)
  851.    (1 3 4 5)
  852.    ? (add-new '(3 4 5) 4)
  853.    (3 4 5)
  854.  
  855. Page 105
  856.  
  857.    ? (add-new! (list 3 4 5) 1)
  858.    (1 3 4 5)
  859.    ? (add-new! (list 3 4 5) 4)
  860.    (3 4 5)
  861.  
  862. Page 106
  863.  
  864.    ? (remove '(3 1 4 1 5 9) 1)
  865.    (3 4 5 9)
  866.  
  867. Page 106
  868.  
  869.    ? (remove! (list 3 1 4 1 5 9) 1)
  870.    (3 4 5 9)
  871.  
  872. Page 106
  873.  
  874.    ? (choose even? '(3 1 4 1 5 9))
  875.    (4)
  876.  
  877. Page 106
  878.  
  879.    ? (choose-by even? (range from: 1)
  880.                       '(a b c d e f g h i))
  881.    (b d f h)
  882.  
  883. Page 107
  884.  
  885.    ? (intersection '(john paul george ringo)
  886.                    '(richard george edward charles john))
  887.    (john george)
  888.  
  889. Page 107
  890.  
  891.    ? (union '(butter flour sugar salt eggs)
  892.             '(eggs butter mushrooms onions salt))
  893.    (salt butter flour sugar eggs mushrooms onions)
  894.  
  895. Page 107
  896.  
  897.    ? (remove-duplicates '(spam eggs spam sausage spam spam spam))
  898.    (spam eggs sausage)
  899.  
  900. Page 108
  901.  
  902.    ? (remove-duplicates! '(spam eggs spam sausage spam spam))
  903.    (spam eggs sausage)
  904.    
  905. Page 108
  906.    
  907.    ? (define hamlet '(to be or not to be))
  908.    hamlet
  909.    ? (id? hamlet (copy-sequence hamlet))
  910.    #f
  911.    ? (copy-sequence hamlet start: 2 end: 4)
  912.    (or not)
  913.  
  914. Page 108
  915.  
  916.    ? (concatenate-as <string> '(#\n #\o #\n) '(#\f #\a #\t))
  917.    "nonfat"
  918.    ? (concatenate-as <vector> '(0 1 2) '(3 4 5) '(6 7 8))
  919.    #(0 1 2 3 4 5 6 7 8)
  920.  
  921. Page 108
  922.  
  923.    ? (concatenate "low-" "calorie")
  924.    "low-calorie"
  925.    ? (concatenate '(0 1 2) '(3 4 5) '(6 7 8))
  926.    (0 1 2 3 4 5 6 7 8)
  927.  
  928. Page 109
  929.  
  930.    ? (define phrase "I hate oatmeal.")
  931.    phrase
  932.    ? (replace-subsequence! phrase "like" start: 2)
  933.    "I like oatmeal."
  934.  
  935.  
  936. Page 109
  937.  
  938.    ? (define x '(bim bam boom))
  939.    x
  940.    ? (reverse x)
  941.    (boom bam bim)
  942.    ? x
  943.    (bim bam boom)
  944.  
  945.  
  946. Page 109
  947.  
  948.    ? (reverse! '(bim bam boom))
  949.    (boom bam bim)
  950.  
  951. Page 110
  952.  
  953.    ? (define numbers '(3 1 4 1 5 9))
  954.    numbers
  955.    ? (sort numbers)
  956.    (1 1 3 4 5 9)
  957.    ? numbers
  958.    (3 1 4 1 5 9)
  959.  
  960. Page 110
  961.  
  962.    ? (sort! '(3 1 4 1 5 9))
  963.    (1 1 3 4 5 9)
  964.  
  965. Page 110
  966.  
  967.    ? (last '(emperor of china))
  968.    china
  969.  
  970. Page 111
  971.  
  972.    ? (subsequence-position "Ralph Waldo Emerson" "Waldo")
  973.    6
  974.  
  975. Page 113
  976.  
  977.    ? (aref #(7 8 9) 1)
  978.    8
  979.  
  980. Page 113
  981.  
  982.    ? (set! (aref #(7 8 9) 1) 5)
  983.    #(7 5 9)                        ;buggy example.  Should return 5
  984.    ? ((setter aref) #(7 8 9) 1 5)
  985.    #(7 5 9)                        ;buggy example.  Should return 5
  986.  
  987. Page 113
  988.  
  989.    ? (dimensions (make <array> dimensions: '(4 4)))
  990.    (4 4)
  991.  
  992. Page 115
  993.  
  994.    ? (cons 1 2)
  995.    (1 . 2)
  996.    ? (cons 1 '(2 3 4 5))
  997.    (1 2 3 4 5)
  998.  
  999. Page 115
  1000.  
  1001.    ? (list 1 2 3)
  1002.    (1 2 3)
  1003.    ? (list (+ 4 3) (- 4 3))
  1004.    (7 1)
  1005.  
  1006. Page 115
  1007.  
  1008.    ? (list* 1 2 3 '(4 5 6))
  1009.    (1 2 3 4 5 6)
  1010.  
  1011.  
  1012. Page 116
  1013.  
  1014.    ? (car '(4 5 6))
  1015.    4
  1016.    ? (car '())
  1017.    ()
  1018.  
  1019. Page 116
  1020.  
  1021.    ? (cdr '(4 5 6))
  1022.    (5 6)
  1023.    ? (cdr '())
  1024.    ()
  1025.  
  1026. Page 116
  1027.  
  1028.    ? (define x '(4 5 6))
  1029.    (4 5 6)
  1030.    ? (set! (car x) 9)
  1031.    9
  1032.  
  1033. Page 116
  1034.  
  1035.    ? (define x '(4 5 6))
  1036.    (4 5 6)
  1037.    ? (set! (cdr x) '(a b c))
  1038.    (a b c)
  1039.  
  1040. Page 120
  1041.  
  1042.    ? (define x "Van Gogh")
  1043.    x
  1044.    ? (as-lowercase x)
  1045.    "van gogh"
  1046.  
  1047. Page 120
  1048.  
  1049.    ? (define x "Van Gogh")
  1050.    x
  1051.    ? (as-lowercase! x)
  1052.    "van gogh"
  1053.  
  1054. Page 120
  1055.  
  1056.    ? (define x "Van Gogh")
  1057.    x
  1058.    ? (as-uppercase x)
  1059.    "VAN GOGH"
  1060.  
  1061. Page 120
  1062.  
  1063.    ? (define x "Van Gogh")
  1064.    x
  1065.    ? (as-uppercase x)
  1066.    "VAN GOGH"
  1067.  
  1068. Page 123
  1069.  
  1070.    (define-method do1 (f (c <collection>))
  1071.      (for ((state (initial-state c) (next-state c state)))
  1072.           ((not state) #f)
  1073.        (f (current-element c state))))
  1074.  
  1075. Page 125
  1076.  
  1077.    (define-method key-sequence ((c <explicit-key-collection>))
  1078.      (for ((state (initial-state c) (next-state c state))
  1079.            (keys  '()               (cons (current-key c state)
  1080.                                           keys)))
  1081.           ((not state) keys)))
  1082.  
  1083. Page 125
  1084.  
  1085.    (define-method do-with-keys (f (c <explicit-key-collection>))
  1086.      (for ((state (initial-state c) (next-state c state)))
  1087.           ((not state) #f)
  1088.        (f (current-key c state) (current-element c state))))
  1089.  
  1090. Page 126
  1091.  
  1092.    (define-method do-with-keys (f (c <sequence>))
  1093.      (for ((state (initial-state c) (next-state c state))
  1094.            (key   0                 (+ key 1)))
  1095.           ((not state) #f)
  1096.        (f key (current-element c state))))
  1097.  
  1098. Page 126
  1099.  
  1100.    (bind ((no-default (cons #f #f)))
  1101.    
  1102.     (define-method .i.element; ((c <explicit-key-collection>) key
  1103.                             #key (default no-default))
  1104.      (for ((state (initial-state c) (next-state c state)))
  1105.           ((or (not state) (= (current-key c state) key))
  1106.            (if state (current-element c state)
  1107.                (if (id? default no-default)
  1108.                    (error ...)
  1109.                    default)))))
  1110.     (define-method .i.element; ((c <sequence>) key
  1111.                             #key (default no-default))
  1112.       (for ((state (initial-state c) (next-state c state))
  1113.             (k     0                 (+ k 1)))
  1114.            ((or (not state) (= k key))
  1115.             (if state (current-element c state)
  1116.                 (if (id? default no-default)
  1117.                     (error ...)
  1118.                     default))))) )
  1119.  
  1120. Page 128
  1121.  
  1122.    (define-method (setter element) ((cÊ<mutable-sequence>)
  1123.                                     (keyÊ<integer>) new-value)
  1124.      (for ((state (initial-state c) (next-state c state))
  1125.            (k     0                 (+ k 1)))
  1126.           ((or (not state) (= k key))
  1127.            (if state
  1128.                (set! (current-element c state) new-value)
  1129.                (error ...)))))
  1130.  
  1131. Page 128
  1132.  
  1133.    (define-method (setter element) ((c <mutable-explicit-key-collection>)
  1134.                                     key new-value)
  1135.      (for ((state (initial-state c) (next-state c state)))
  1136.           ((or (not state) (= (current-key c state) key))
  1137.            (if state
  1138.                (set! (current-element c state) new-value)
  1139.                (error ...)))))
  1140.  
  1141. Page 129
  1142.  
  1143.    (define-method do2 (f (c1 <collection>) (c2 <collection>))
  1144.      (bind ((keys (intersection (key-sequence c1)
  1145.                                 (key-sequence c2))))
  1146.        (for ((ks (initial-state keys) (next-state keys ks)))
  1147.             ((not ks) #f)
  1148.          (bind ((key (current-element keys ks)))
  1149.            (f (element c1 key) (element c2 key))))))
  1150.  
  1151. Page 129
  1152.  
  1153.    (define-method do2 (f (c1 <sequence>) (c2 <sequence>))
  1154.      (for ((s1 (initial-state c1) (next-state c1 s1))
  1155.            (s2 (initial-state c2) (next-state c2 s2)))
  1156.           ((or (not s1) (not s2)) #f)
  1157.        (f (current-element c1 s1) (current-element c2 s2))))
  1158.  
  1159. Page 130
  1160.  
  1161.    (define-method map-into1 ((target <mutable-collection>) f
  1162.                              (source <collection>))
  1163.      (bind ((keys (intersection (key-sequence target)
  1164.                                 (key-sequence source))))
  1165.        (for ((ks (initial-state keys) (next-state keys ks)))
  1166.             ((not ks) target)
  1167.          (bind ((key (current-element keys ks)))
  1168.            (set! (element target key) (f (element source key)))))))
  1169.    (define-method map-into1 ((target <mutable-sequence>) f
  1170.                              (source <sequence>))
  1171.      (for ((ss (initial-state source) (next-state source ss))
  1172.            (ts (initial-state target) (next-state target ts)))
  1173.           ((or (not ss) (not ts)) target)
  1174.        (set! (current-element target ts)
  1175.              (f (current-element source ss)))))
  1176.  
  1177. Page 142
  1178.  
  1179.    (handler-case (some-function)
  1180.      ((<type-error>) "there was a type-error")
  1181.      ((<error>) "there was an error")
  1182.      ((<warning>) "there was a warning"))
  1183.  
  1184. Page 144-146
  1185.  
  1186.    ;;; Classes such as <file-not-found> used in these examples are
  1187.    ;;; invented for the example and are not part of the specification
  1188.    ;;; This example shows minimal handling of a file-not-found error
  1189.    
  1190.    (handler-case (open "file-that-doesnt-exist")
  1191.      ((<file-not-found> condition: c
  1192.        (format *error-output* "~&The file ~A was not found."
  1193.                (file-name c))))
  1194.    
  1195.    
  1196.    ;;; This example shows how to handle a file-not-found error by
  1197.    ;;; reading a different file instead.
  1198.    (handler-bind (<file-not-found>
  1199.                     (method (condition next-handler)
  1200.                       (signal (make <try-a-different-file>
  1201.                                     file-name: "my-emergency-backup-file"))))
  1202.       (open "file-that-doesnt-exist")
  1203.      ....)
  1204.    
  1205.    (define-method open (the-file)
  1206.      (handler-case (guts-of-open the-file)
  1207.        ((<try-a-different-file>
  1208.          description: (method (stream)
  1209.                         (format stream "Read a different file instead of ~A"
  1210.                                         the-file))
  1211.          condition: restart
  1212.         (open (file-name restart)))))))
  1213.    
  1214.    (define-method guts-of-open (the-file)
  1215.      (bind ((result (operating-system-open the-file)))
  1216.        (cond ((instance? result <stream>) result)
  1217.              ((id? result +file-not-found-error-code+)
  1218.               (error (make <file-not-found> file-name: the-file)))
  1219.              ...)))
  1220.    
  1221.    (define-class <file-not-found> (<error>)
  1222.      ((file-name init-keyword: file-name:)))
  1223.    
  1224.    (define-method print ((self <file-not-found>) #key stream verbose)
  1225.      (if verbose
  1226.          (next-method)
  1227.          (format stream "The file ~A was not found" (file-name self))))
  1228.    
  1229.    (define-class <try-a-different-file> (<restart>)
  1230.      ((file-name init-keyword: file-name:)))
  1231.    
  1232.    
  1233.    ;;; This is the same example improved so the restart handler that
  1234.    ;;; reads another file can only be reached by a handler for the
  1235.    ;;; associated condition, useful if there are nested errors.
  1236.    
  1237.    (handler-bind (<file-not-found>)
  1238.                     (method (condition next-handler)
  1239.                       (signal (make <try-a-different-file>
  1240.                                     condition: condition
  1241.                                     file-name: "my-emergency-backup-file")))
  1242.      (open "file-that-doesnt-exist")
  1243.      ....)
  1244.    
  1245.    (define-method open (the-file)
  1246.      ....  (guts-of-open the-file))
  1247.    
  1248.    (define-method guts-of-open (the-file)
  1249.      (bind ((result (operating-system-open the-file)))
  1250.        (cond ((instance? result <stream>) result)
  1251.              ((id? result +file-not-found-error-code+)
  1252.               (bind ((condition (make <file-not-found> file-name: the-file)))
  1253.                 (handler-case (error condition)
  1254.                   ((<try-a-different-file>
  1255.                      test: (compose (curry id? condition) restart-condition)
  1256.                     description: (method (stream)
  1257.                                    (format stream
  1258.                                      "Read a different file instead of ~A"
  1259.                                      the-file))
  1260.                     condition: restart
  1261.                    (open (file-name restart)))))))
  1262.              ...)))
  1263.    
  1264.    (define-class <file-not-found> (<error>)
  1265.      ((file-name init-keyword: file-name:)))
  1266.    
  1267.    (define-method print ((self <file-not-found>) #key stream verbose)
  1268.      (if verbose
  1269.         (next-method)
  1270.         (format stream "The file ~A was not found" (file-name self))))
  1271.    
  1272.    (define-class <try-a-different-file> (<restart>)
  1273.      ((condition init-keyword: condition: reader: restart-condition)
  1274.       (file-name init-keyword: file-name:)))
  1275.  
  1276. Page 153
  1277.  
  1278.    ? (as <symbol> "foo")
  1279.    foo
  1280.    ? (id? 'FOO (as <symbol> "Foo"))
  1281.    #t
  1282.    ? 'Foo
  1283.    foo
  1284.    ? (as <keyword> "foo")
  1285.    foo:
  1286.  
  1287. Page 154
  1288.  
  1289.    ? (as <string> 'Foo)
  1290.    "foo"
  1291.    ? (as <string> 'bar:)
  1292.    "bar"
  1293.  
  1294. Page 157
  1295.  
  1296.    ? (define-method sum ((numbers <sequence>))
  1297.          (reduce1 + numbers))
  1298.    sum
  1299.    ? (define-method square ((x <number>)) (* x x))
  1300.    square
  1301.    ? (define-method square-all ((coords <sequence>))
  1302.        (map square coords))
  1303.    square-all
  1304.    ? (define distance (compose sqrt sum square-all))
  1305.    distance
  1306.    ? (distance '(3 4 5))
  1307.    7.0710678118654755
  1308.  
  1309. Page 157
  1310.  
  1311.    ? (map female? '(michelle arnold roseanne))
  1312.    (#t #f #t)
  1313.    ? (map (complement female?) '(michelle arnold roseanne))
  1314.    (#f #t #f)
  1315.  
  1316. Page 158
  1317.  
  1318.    ? (map (curry + 1) '(3 4 5))
  1319.    (4 5 6)
  1320.  
  1321. Page 158
  1322.  
  1323.    ? (define yuppify (rcurry concatenate ", ayup"))
  1324.    yuppify
  1325.    ? (yuppify "I'm from New Hampsha")
  1326.    "I'm from New Hampsha, ayup"
  1327.  
  1328. Page 159
  1329.  
  1330.    ? ((always 1) 'x 'y 'z)
  1331.    1
  1332.    ? ((always #t) #f #f)
  1333.    #t
  1334.